# -*- coding: utf-8 -*-
"""
统一响应格式工具类
类似于Java的ResultVo
"""
from flask import jsonify
from typing import Any, Optional, Union

class ResponseCode:
    """响应状态码常量"""
    SUCCESS = 200
    BAD_REQUEST = 400
    UNAUTHORIZED = 401
    FORBIDDEN = 403
    NOT_FOUND = 404
    INTERNAL_ERROR = 500

    # 业务状态码
    LOGIN_FAILED = 1001
    TOKEN_INVALID = 1002
    USER_NOT_FOUND = 1003
    PERMISSION_DENIED = 1004
    VALIDATION_ERROR = 1005

class Result:
    """统一响应结果类"""

    def __init__(self, code: int, message: str, data: Any = None):
        self.code = code
        self.message = message
        self.data = data

    def to_dict(self) -> dict:
        """转换为字典格式"""
        result = {
            'code': self.code,
            'message': self.message
        }

        if self.data is not None:
            result['data'] = self.data

        return result

    def to_response(self, http_code: int = 200):
        """转换为Flask响应对象"""
        return jsonify(self.to_dict()), http_code

    @classmethod
    def success(cls, data: Any = None, message: str = "操作成功"):
        """成功响应"""
        return cls(ResponseCode.SUCCESS, message, data)

    @classmethod
    def error(cls, code: int = ResponseCode.INTERNAL_ERROR, message: str = "操作失败", data: Any = None):
        """错误响应"""
        return cls(code, message, data)

    @classmethod
    def bad_request(cls, message: str = "请求参数错误", data: Any = None):
        """400错误响应"""
        return cls(ResponseCode.BAD_REQUEST, message, data)

    @classmethod
    def unauthorized(cls, message: str = "未授权访问", data: Any = None):
        """401错误响应"""
        return cls(ResponseCode.UNAUTHORIZED, message, data)

    @classmethod
    def forbidden(cls, message: str = "权限不足", data: Any = None):
        """403错误响应"""
        return cls(ResponseCode.FORBIDDEN, message, data)

    @classmethod
    def not_found(cls, message: str = "资源未找到", data: Any = None):
        """404错误响应"""
        return cls(ResponseCode.NOT_FOUND, message, data)

    @classmethod
    def internal_error(cls, message: str = "服务器内部错误", data: Any = None):
        """500错误响应"""
        return cls(ResponseCode.INTERNAL_ERROR, message, data)

    @classmethod
    def login_failed(cls, message: str = "登录失败", data: Any = None):
        """登录失败响应"""
        return cls(ResponseCode.LOGIN_FAILED, message, data)

    @classmethod
    def token_invalid(cls, message: str = "令牌无效", data: Any = None):
        """令牌无效响应"""
        return cls(ResponseCode.TOKEN_INVALID, message, data)

    @classmethod
    def validation_error(cls, message: str = "数据验证失败", data: Any = None):
        """数据验证失败响应"""
        return cls(ResponseCode.VALIDATION_ERROR, message, data)

# 便捷方法
def success_response(data: Any = None, message: str = "操作成功", http_code: int = 200):
    """返回成功响应"""
    return Result.success(data, message).to_response(http_code)

def error_response(code: int = ResponseCode.INTERNAL_ERROR, message: str = "操作失败",
                  data: Any = None, http_code: int = 500):
    """返回错误响应"""
    return Result.error(code, message, data).to_response(http_code)

def bad_request_response(message: str = "请求参数错误", data: Any = None):
    """返回400错误响应"""
    return Result.bad_request(message, data).to_response(400)

def unauthorized_response(message: str = "未授权访问", data: Any = None):
    """返回401错误响应"""
    return Result.unauthorized(message, data).to_response(401)

def forbidden_response(message: str = "权限不足", data: Any = None):
    """返回403错误响应"""
    return Result.forbidden(message, data).to_response(403)

def not_found_response(message: str = "资源未找到", data: Any = None):
    """返回404错误响应"""
    return Result.not_found(message, data).to_response(404)

def internal_error_response(message: str = "服务器内部错误", data: Any = None):
    """返回500错误响应"""
    return Result.internal_error(message, data).to_response(500)

def login_failed_response(message: str = "用户名或密码错误", data: Any = None):
    """返回登录失败响应"""
    return Result.login_failed(message, data).to_response(401)

def token_invalid_response(message: str = "令牌无效或已过期", data: Any = None):
    """返回令牌无效响应"""
    return Result.token_invalid(message, data).to_response(401)

def validation_error_response(message: str = "数据验证失败", data: Any = None):
    """返回数据验证失败响应"""
    return Result.validation_error(message, data).to_response(400)